﻿using System;

namespace Wintellect.LiveFx.Cloudboard
{
  public enum AvailableHotkeys
  {
    A = System.Windows.Input.Key.A,
    B,
    C,
    D,
    E,
    F,
    G,
    H,
    I,
    J,
    K,
    L,
    M,
    N,
    O,
    P,
    Q,
    R,
    S,
    T,
    U,
    V,
    W,
    X,
    Y,
    Z
  }

  internal static class InputKeyExtensions
  {
    public static KeyCode ToKeyCode(this AvailableHotkeys availableKey)
    {
      return (KeyCode)(((int)availableKey - (int)AvailableHotkeys.A) + (int)KeyCode.A);
    }
    public static AvailableHotkeys FromKeyCode(this KeyCode keyCode)
    {
      return (AvailableHotkeys)(((int)keyCode - (int)KeyCode.A) + (int)AvailableHotkeys.A);
    }

    public static System.Windows.Input.Key ToInputKey(this AvailableHotkeys availableKey)
    {
      return (System.Windows.Input.Key)(((int)availableKey - (int)AvailableHotkeys.A) + (int)System.Windows.Input.Key.A);
    }
    public static AvailableHotkeys FromInputKey(this System.Windows.Input.Key inputKey)
    {
      return (AvailableHotkeys)(((int)inputKey - (int)System.Windows.Input.Key.A) + (int)AvailableHotkeys.A);
    }
  }

  internal static class KeyCodeExtensions
  {
    public static KeyCode ToKeyCode(this System.Windows.Input.Key inputKey)
    {
      return (KeyCode)(((int)inputKey - (int)System.Windows.Input.Key.A) + (int)KeyCode.A);
    }
    public static System.Windows.Input.Key ToInputKey(this KeyCode keyCode)
    {
      return (System.Windows.Input.Key)(((int)keyCode - (int)KeyCode.A) + (int)System.Windows.Input.Key.A);
    }
  }

  public struct Hotkey
  {
    public bool Control { get; set; }
    public bool Shift { get; set; }
    public bool Alt { get; set; }
    public System.Windows.Input.Key Key { get; set; }

    public int ToInt32()
    {
      int output = 0;
      output |= (this.Control ? 1 : 0) << 24;
      output |= (this.Alt ? 1 : 0) << 16;
      output |= (this.Shift ? 1 : 0) << 8;
      output |= (Byte)this.Key;
      return output;
    }

    public override int GetHashCode()
    {
      return this.ToInt32();
    }

    public override bool Equals(object obj)
    {
      if (!(obj is Hotkey)) return false;
      return this.ToInt32() == ((Hotkey)obj).ToInt32();
    }

    public static Hotkey FromInt32(int value)
    {
      Hotkey output = new Hotkey();
      output.Control |= (value & 0x01000000) != 0;
      output.Alt = (value & 0x010000) != 0;
      output.Shift = (value & 0x0100) != 0;
      System.Windows.Input.Key key = (System.Windows.Input.Key)(value & 0xFF);
      output.Key = key;
      return output;
    }

    public static bool operator ==(Hotkey a, Hotkey b)
    {
      return a.ToInt32() == b.ToInt32();
    }
    public static bool operator !=(Hotkey a, Hotkey b)
    {
      return a.ToInt32() != b.ToInt32();
    }
  }
}
